<?php
/*
	Copyright (C) 2008 - 2013 Inanta Martsanto
	Inanta Martsanto (inanta@inationsoft.com)

	This file is part of NewStep Framework.

	NewStep Framework is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	NewStep Framework is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with NewStep Framework.  If not, see <http://www.gnu.org/licenses/>.
*/

namespace Inationsoft\NS\Utility;

use Inationsoft\NS\Iterable;
use Inationsoft\NS\Exception\IOException;

/**
 *CSV (Comma-separated values) file processing
 *
 *@author Inanta Martsanto <inanta@inationsoft.com>
 */
class CSV extends Iterable {
	private $_columns;

	function __construct($filename, $separator = ',') {
		if(!is_readable($filename)) {
			if(!is_file($filename)) throw new IOException(array('code' => IOException::FILE_NOT_FOUND, 'filename' => $filename));

			throw new IOException(array('code' => IOException::FILE_NOT_READABLE, 'filename' => $filename));
		}

		$rows = explode("\n", str_replace(array("\n\r", "\r") , "\n", file_get_contents($filename)));
		$this->_columns = $rows;

		$this->_columns =  preg_split("/" . $separator . "(?!(?:[^\\\"" . $separator . "]|[^\\\"]" . $separator . "[^\\\"])+\\\")/", $rows[0]);
		unset($rows[0]);
		
		foreach($this->_columns as $index => $column) {
			$this->_columns[$index] = preg_replace("/[^A-Za-z0-9]/", '', $column);
		}

		foreach($rows as $key => $row) {
			$columns = preg_split("/" . $separator . "(?!(?:[^\\\"" . $separator . "]|[^\\\"]" . $separator . "[^\\\"])+\\\")/", $row);
			foreach($columns as $index => $column) {
				$this->_collection[$key - 1][$this->_columns[$index]] = $column;
			}
		}

		$this->initialize();
	}

	function getColumns() {
		return $this->_columns;
	}

	function __get($k) {
		if(isset($this->_columns[$k])) return ($this->_data[$this->_rowIterator][$this->_columns[$k]]);

		return parent::__get($k);
	}

	function __set($k, $v) {
		if(isset($this->_columns[$k])) {
			$this->_data[$this->_rowIterator][$this->_columns[$k]] = $v;
		} else {
			parent::__set($k, $v);
		}
	}
}
?>